﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Bamboo.CodeCat
{
    public static class Utils
    {
        /// <summary>
        /// 修复名称中的问题
        /// 把数字和字母之外的字符都替换成下划线, 且保证不会出现连续的下划线
        /// 如果首字符不是字母, 则首字符补下划线
        /// </summary>
        /// <param name="src"></param>
        /// <returns></returns>
        public static string FixName(string src)
        {
            if (string.IsNullOrWhiteSpace(src)) return "";
            src = src.Trim();
            StringBuilder buffer = new StringBuilder(src.Length);
            buffer.Clear();
            bool hasUnderLine = false;
            if (src.Length > 0 && !(src[0] >= 'a' && src[0] <= 'z' || src[0] >= 'A' && src[0] <= 'Z'))
            {
                buffer.Append('_');
                hasUnderLine = true;
            }

            foreach (char ch in src)
            {
                if (ch >= '0' && ch <= '9' || ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z')
                {
                    buffer.Append(ch);
                    hasUnderLine = false;
                }
                else
                {
                    if (hasUnderLine)
                    {

                    }
                    else
                    {
                        hasUnderLine = true;
                        buffer.Append('_');
                    }
                }
            }
            return buffer.ToString();
        }


        /// <summary>
        /// 获取驼峰形式的名称, 开头大写.
        /// 转换后仅保留字母+数字
        /// </summary>
        /// <param name="src"></param>
        /// <returns></returns>
        public static string ToCamelName(string src)
        {
            if (string.IsNullOrWhiteSpace(src)) return "";
            src = src.Trim();
            StringBuilder buffer = new StringBuilder(src.Length);
            buffer.Clear();
            bool bigLetter = true;
            //如果首字符不是字母,补个大写C上去
            if (src.Length > 0 && !(src[0] >= 'a' && src[0] <= 'z' || src[0] >= 'A' && src[0] <= 'Z'))
            {
                buffer.Append('C');
                bigLetter = false;
            }

            foreach (char ch in src)
            {
                if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z')
                {
                    if (bigLetter)
                    {
                        buffer.Append(char.ToUpper(ch));
                    }
                    else
                    {
                        buffer.Append(char.ToLower(ch));
                    }
                    bigLetter = false;
                }
                else if (ch >= '0' && ch <= '9')
                {
                    buffer.Append(ch);
                    bigLetter = true;
                }
                else
                {
                    bigLetter = true;
                }
            }
            return buffer.ToString();
        }


        public static string ToCamelNameFirstSmall(string src)
        {
            if (string.IsNullOrWhiteSpace(src)) return "";
            string camelName = ToCamelName(src);
            string camelNameFirstSmall;
            if (camelName[0] >= 'A' && camelName[0] <= 'Z')
                camelNameFirstSmall = (char)(camelName[0] - 'A' + 'a') + camelName.Substring(1);
            else
                camelNameFirstSmall = camelName;
            return camelNameFirstSmall;
        }


        /// <summary>
        /// 获得小写+下划线形式的名称
        /// </summary>
        /// <param name="src"></param>
        /// <returns></returns>
        public static string ToLowerName(string src)
        {
            if (string.IsNullOrWhiteSpace(src)) return "";
            string text = FixName(src).ToLower();
            return text;
        }

        /// <summary>
        /// 获得大写+下划线形式的名称
        /// </summary>
        /// <param name="src"></param>
        /// <returns></returns>
        public static string ToUpperName(string src)
        {
            if (string.IsNullOrWhiteSpace(src)) return "";
            string text = FixName(src).ToUpper();
            return text;
        }


        public static void CheckDir(params string[] paths)
        {
            string fullpath = Path.Combine(paths);
            if (!Directory.Exists(fullpath))
            {
                Directory.CreateDirectory(fullpath);
            }
        }


    }
}
